package com.lienlq.voicemusic.entity;
import java.util.List; 

import com.lienlq.voicemusic.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TrackAdapter extends ArrayAdapter<Track>{
	
	private List<Track> tracks = null;
	private Context mContext;
	
	public TrackAdapter(Context context, int textViewResourceId,
			List<Track> objects, List<Track> tracks, Context mContext) {
		super(context, textViewResourceId, objects);
		this.tracks = tracks;
		this.mContext = mContext;
	}
 
	  
	public TrackAdapter(Context context, int resource, 	List<Track> objects) {
		super(context, resource,  objects); 
		tracks = objects;
		mContext = context;
	}
 
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		TrackHolder holder = null;
		
		if (v == null) {
	        LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        v = vi.inflate(R.layout.item_track, null);
	        holder = new TrackHolder(v);
	        v.setTag(holder);
	        Log.i("ListView", "TrackApdapter is loaded at " + position);
		} else {
			holder = (TrackHolder)v.getTag();
		}
  
        holder.populateFrom(tracks.get(position));
  
//         if(track != null) {
//		  TextView songItemTV  = (TextView)v.findViewById(R.id.track_item);
//		  TextView artistItemTV = (TextView)v.findViewById(R.id.artist_item);
//		  TextView numberListenItemTV = (TextView)v.findViewById(R.id.number_listen_item);
//		  
//		  songItemTV.setText(track.getTrack());
//		  artistItemTV.setText(track.getArtist());
//		  numberListenItemTV.setText(""+track.getNumberListen());
//		  Log.i("ListView", "TrackApdapter is loaded at " + position);
//         }
		return v;
		
	}
	
	static class TrackHolder {
		private TextView track, artist, amount;
		private ImageView playImg;
		
		TrackHolder(View row) {
			track = (TextView)row.findViewById(R.id.track_item);
			artist = (TextView)row.findViewById(R.id.artist_item);
			amount = (TextView)row.findViewById(R.id.number_listen_item); 
			playImg = (ImageView)row.findViewById(R.id.play_img_item);
		}
		
		
		void populateFrom(Track t) {
			track.setText(t.getTrack());
			artist.setText(t.getArtist());
			amount.setText("" + t.getNumberListen());
			playImg.setImageResource(R.drawable.item_play);
		}
	}
	
}